<?php

namespace App\Http\Controllers\Payment;

use App\Http\Controllers\Controller;
use App\Models\SellerWithdrawRequest;
use App\Models\Wallet;
use App\Models\Payment;
use App\Models\User;
use Illuminate\Http\Request;

class UdunController extends Controller
{


    public function uduncall(Request $request)
    {
        $input = $request->input();
        if (isset($input['body']) ){
            $data=json_decode($input['body'],true);
            if($data['tradeType']==1){
                $mainCoinType=$data['mainCoinType'];
                $coinType=$data['coinType'];
                $coin_name="";
                /*if($mainCoinType=="0" ){
                    $coin_name="wallet_btc";
                }else */if($mainCoinType=="195" && $coinType=="TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t"){
                    $coin_name="wallet_usdt_trc";
                }
                else if($mainCoinType=="60" && $coinType=="0xdac17f958d2ee523a2206206994597c13d831ec7"){
                    $coin_name="wallet_usdt_erc";
                }
                else{
                    return 'error001';
                }
                if($coin_name){
                     $user = User::where($coin_name, $data['address'])->first();
                    if (!$user) {
                        return 'not user';
                    }
                    $wallet_order = Wallet::where("payment_details",$data['txId'])->first();
                    if($wallet_order){
                        return 'txid已经存在';
                    }
                    $amount=$data['amount']/pow(10,$data['decimals']);
                    $wallet = new Wallet;
                    $wallet->user_id = $user->id;
                    $wallet->amount = $amount;
                    $wallet->payment_method = "usdt";
                    $wallet->payment_details = $data['txId'];
                    $wallet->approval = 1;//直接状态为已审核
                    $wallet->offline_payment = 1;
                    $wallet->reciept = '';
                    $wallet->operator_id = $user->id;
                    $wallet->type = 1;
                    $wallet->save();
                    $user->balance = $user->balance + $amount;
                    $user->save();
                    if ( $wallet->save() && $user->save() )
                    {
                        return 'ok';
                    }
                }
            
            }
            else if($data['tradeType']==2){//提币
                 if ($data['status'] == '3'){
                     
                    $seller_withdraw_request = SellerWithdrawRequest::findOrFail($data['businessId']);
                    if ($seller_withdraw_request){
                        $seller_withdraw_request->status = '1';
                        $seller_withdraw_request->viewed = '1';
                        $seller_withdraw_request->save();
                        
                        $user = User::find($seller_withdraw_request->user_id);
                        if (!$user) {
                             return 'error';
                        }
                         $payment = new Payment;
                        $payment->seller_id = $user->id;
                        $payment->amount = $seller_withdraw_request['amount'];
                        $payment->payment_method = "usdt_payment";
                        $payment->txn_code = $data['txid'];
                        $payment->currency = '';
                        $payment->payment_details = $data['mainCoinType'].$data['coinType'];
                        $payment->t_type = 1;
                        $payment->save();
                    }
                
    
                    return 'ok';
                }
            }
            
        }
        return 'error';
    }



}
